package com.ccx.web.backand;

import com.ccx.model.EbNews;
import com.ccx.service.NewsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/News")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        System.out.println("进入");
        List<EbNews> ebNews = newsService.list(page, pageSize);
        model.addAttribute("ebNews", ebNews);

        model.addAttribute("curPage",page);
        //2.2 传递最大页到product
        model.addAttribute("maxPage",((Page)ebNews).getPages());

        return "manage/news";
    }

    @RequestMapping("/to_save")
    public  String toSave(){
        return "manage/news-add";
    }


    @RequestMapping("/save")
    public String save(EbNews ebNews) {
        System.out.println("ebNews==》 " + ebNews);
        Integer result = newsService.save(ebNews);
        if(result<1) {
            //添加失败
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }




    @RequestMapping("/to_edit")
    public  String toEdit(Model model,@RequestParam("enid")int enId){
        System.out.println("点击修改跳过来，enid="+enId);
        EbNews ebNews = newsService.select0ne(enId);
        //2:指定model和jsp
        model.addAttribute("newNews",ebNews);
        return  "manage/news-modify";
    }

    @RequestMapping("/edit")
    public  String edit(EbNews ebNews){
        System.out.println("ebNews==>"+ebNews);
        Integer result =newsService.updateNews(ebNews);
        if(result<1){
            return "manage/manage-error";
        }
        return  "manage/manage-result";
    }

    @RequestMapping("/delete")
    public  String DeleteNews(EbNews ebNews){
        int i = newsService.delete(ebNews);
        System.out.println(ebNews.getEnId());
        if (i<1){
            return "manage/manage-error";
        }
        return  "manage/manage-result";
        }
    }
